/**
 * 表单验证
 * 
 * @param {Object} rules 验证字段的规则
 * @param {Object} messages 验证字段的提示信息
 * 
 */

import basic from './basic.js';
var actmodel = new basic();
actmodel.do = "coupon_cont"

actmodel.offset = function (that, reset, para, xcsorts) {
  if (!xcsorts) {
    xcsorts = [
      ["id", "desc"]
    ];
  }

  var acturlpara = {};
  acturlpara["url"] = "entry/wxapp/" + actmodel.do
  if (para) {
    para["op"] = "offset"
  } else {
    this.wherepara["op"] = "offset"
  }
  return actmodel.pagelist(acturlpara, that, reset, para, xcsorts);
}
/**
 * 获取所有优惠券
 */
actmodel.getcoupon = function (couponid) {
  var app = getApp();
  var data = {
    "op": "getcoupon",
    couponid:couponid
  }
  var that = this;
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/' + actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {}
    });
  });
}
/**
 * 获取所有优惠券
 */
actmodel.allcoupon = function () {
  var app = getApp();
  var data = {
    "op": "allcoupon"
  }
  var that = this;
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/' + actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {}
    });
  });
}
/**
 * 获取我的所有优惠券
 */
actmodel.mycoupon = function () {
  var app = getApp();
  var data = {
    "op": "myallcoupon"
  }
  var that = this;
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/' + actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {}
    });
  });
}
/**
 * 验证有效优惠券
 */
actmodel.vlidEffective = function (coupons, orderinfo) {
    var newcpons = [];
    coupons.forEach(item => {
      if (actmodel.vildEffectiveitem(item, orderinfo)) {
        newcpons.push(item)
      }
    })
    return newcpons;
  },
  /**
   * 优惠金额
   */
  actmodel.discountmoney = function (item, orderinfo) {

    var vtotalmone = 0;
    var prolimitcompile = item['prolimitcompile'];
    orderinfo.forEach(item => {
      var itemres=true
      var temprice = (item.cid == "0" ? item.realprice : item.activityprice) * item.num

      switch (prolimitcompile["cid"]) {
        case "1": {

          break;
        }
        case "2": {
          console.log("prolimitcompile")
          console.log(prolimitcompile.allow)
          console.log(prolimitcompile.allow.hasOwnProperty(item.proid))
          if(prolimitcompile.allow ){
            if(!prolimitcompile.allow.hasOwnProperty(item.proid)){
              itemres=false
            }
          }
          break;
        }
        case "3": {
          if(prolimitcompile.disallow){
            if(prolimitcompile.disallow.hasOwnProperty(item.proid)){
              itemres=false
            }
          }
          break;
        }
      }
      if(itemres){
        vtotalmone += temprice
      }


    })


    if (item.cid == "1") {
      return item.val
    } else {
   

      var disval = Math.round(vtotalmone * (100 - item.val) / 100)
   
      return disval
    }
  }

/**验证优惠券是否可用 */
actmodel.vildEffectiveitem = function (item, orderinfo) {
  var vtotalmone = 0;
  var prolimitcompile = item['prolimitcompile'];
  var result=false;
  for(var i in orderinfo){
     var items=orderinfo[i]
     var itemres=true
    //  console.log("index:"+i)
     var temprice = (items.cid == "0" ? items.realprice : items.activityprice) * items.num;
     switch (prolimitcompile["cid"]) {
       case "1": {
 
         break;
       }
       case "2": {
  
         if (prolimitcompile.allow && prolimitcompile.allow.hasOwnProperty(items.id?items.id:items.proid)) {
          itemres=true
         } else {
          itemres=false
         }
 
         break;
       }
       case "3": {
      
         // if(prolimitcompile.disallow &&prolimitcompile.disallow.hasOwnProperty(item.id)){
 
         //   return true
         // }
 
         if (!prolimitcompile.disallow || !prolimitcompile.disallow.hasOwnProperty(items.id?items.id:items.proid)) {
          itemres=true
         } else {
          itemres=false
         }
         break;
       }
     }
     if(itemres){
   
       vtotalmone += temprice
       result=true
     }
     
  }


  if(result){
  
    if (item.hascondition == "2" && parseInt(item.fullmoney) > vtotalmone) {
      return false
    }else{
      return true
    }
  }else{
    return false

  }

  
}
/**
 *  产品可以领取的优惠
 * @param {*} proid 
 */
actmodel.procoupon = function (proid, condition) {
  var app = getApp();
  if (!proid) {
    proid = 0
  }
  var data = {
    "op": "procoupon",
    proid: proid
  }
  if (condition) {
    data["condition"] = JSON.stringify(condition)
  }

  var that = this;
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/' + actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {}
    });
  });
}

/**
 *  广告可以领取的优惠
 * @param {*} proid 
 */
actmodel.adcoupon = function () {
  var app = getApp();
  var data = {
    "op": "adcoupon"
  }
  var that = this;
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/' + actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {}
    });
  });
}


/**
 * 获取领取券
 * @param {*} couponid 
 */
actmodel.receivecoupon = function (couponid,code) {
  var app = getApp();
  var data = {
    "op": "receivecoupon",
    couponid: couponid,
    code:code
  }
  var that = this;
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/' + actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {}
    });
  });
}


/**
 * 获取领取券们
 * @param {*} couponids
 */
actmodel.receiveallcoupon = function (couponids) {
  var app = getApp();
  var data = {
    "op": "receiveallcoupon",
    couponids: couponids
  }
  var that = this;
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/' + actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {}
    });
  });
}



module.exports = actmodel;